part of 'case_stepper.dart';

/// 基本信息
class _BaseInfo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ListBody(
      children: <Widget>[
        _CaseTypeSelect(),
        Gaps.vGap12,
        _CaseContentInput(),
        Gaps.vGap12,
      ],
    );
  }
}

/// 案件类型选择器
class _CaseTypeSelect extends StatelessWidget {
  const _CaseTypeSelect({
    Key key,
  }) : super(key: key);

  /// 选择案件类型
  Future<void> _selectCaseType(BuildContext context) async {
    final option = await showDialog(
      context: context,
      builder: (context) => SimpleDialog(
        title: Text(
          '选择案件类型',
          textAlign: TextAlign.center,
        ),
        children: <Widget>[
          Divider(
            height: 1,
          ),
          ListTile(
            title: Text(
              '类型1',
              style: TextStyles.listContent
                  .copyWith(color: AppColors.PRIMARY_TEXT),
            ),
            onTap: () {
              Navigator.of(context).pop('类型1');
            },
          ),
          Divider(
            height: 1,
          ),
          ListTile(
            title: Text(
              '类型2',
              style: TextStyles.listContent
                  .copyWith(color: AppColors.PRIMARY_TEXT),
            ),
            onTap: () {
              Navigator.of(context).pop('类型2');
            },
          ),
        ],
      ),
    );
    if (option != null) {
      context.bloc<CaseFormBloc>().add(CaseTypeChanged(option));
    }
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<CaseFormBloc, CaseFormState>(
      buildWhen: (previous, current) => previous.caseType != current.caseType,
      builder: (context, state) {
        return Container(
          color: Colors.white,
          child: ListTile(
            title: Text(
              '案件类型',
              style: TextStyles.listTitle,
            ),
            trailing: Row(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Text(
                  context.bloc<CaseFormBloc>().state.caseType.value == ''
                      ? '请选择当时单位'
                      : context.bloc<CaseFormBloc>().state.caseType.value,
                  style: TextStyles.listExtra,
                ),
                Icon(
                  Icons.keyboard_arrow_right,
                  color: AppColors.REGULAR_TEXT,
                ),
              ],
            ),
            onTap: () {
              _selectCaseType(context);
            },
          ),
        );
      },
    );
  }
}

/// 案件内容输入框
class _CaseContentInput extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocBuilder<CaseFormBloc, CaseFormState>(
      buildWhen: (previous, current) =>
          previous.caseContent != current.caseContent,
      builder: (context, state) {
        return Column(
          children: <Widget>[
            Container(
              color: Colors.white,
              child: ListTile(
                title: Text(
                  '案情记录',
                  style: TextStyles.listTitle,
                ),
                trailing: InkResponse(
                  child: Container(
                    child: Icon(
                      Icons.add,
                      color: AppColors.REGULAR_TEXT,
                    ),
                  ),
                  onTap: () {
                    _selectContentFromTemplate(context);
                  },
                ),
              ),
            ),
            Container(
              padding: EdgeInsets.symmetric(horizontal: 16.0),
              color: Colors.white,
              child: TextField(
                key: const Key('case_content_input_textField'),
                onChanged: (caseContent) => context
                    .bloc<CaseFormBloc>()
                    .add(CaseContentChanged(caseContent)),
                maxLines: 3,
                decoration: InputDecoration(
                  hintText: '请输入案情描述',
                  errorText: state.caseContent.invalid ? '请输入案情记录' : null,
                ),
              ),
            ),
          ],
        );
      },
    );
  }

  void _selectContentFromTemplate(BuildContext context) {
    print('选择内容');
  }
}
